#app/models/salary.py
from app import db
from datetime import datetime

class SalaryInfo(db.Model):
    __tablename__ = 'salary_info'

    id = db.Column(db.BigInteger, primary_key=True)
    user_id = db.Column(db.BigInteger, nullable=False)
    company = db.Column(db.String(100), nullable=False)
    position = db.Column(db.String(100), nullable=False)
    city = db.Column(db.String(50), nullable=False)
    base_salary = db.Column(db.Numeric(10,2), nullable=False)
    months = db.Column(db.Integer, default=12)
    bonus = db.Column(db.Numeric(10,2), default=0)
    signing_bonus = db.Column(db.Numeric(10,2), default=0)
    stock = db.Column(db.String(100))
    type = db.Column(db.Enum('校招','实习','社招'), default='校招')
    education = db.Column(db.Enum('大专','本科','硕士','博士'))
    school_tier = db.Column(db.Integer)
    industry = db.Column(db.String(50))
    tags = db.Column(db.JSON)
    remark = db.Column(db.Text)
    privacy_level = db.Column(db.Integer, default=1)
    status = db.Column(db.Enum('pending','approved','rejected'), default='pending')
    view_count = db.Column(db.Integer, default=0)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    def to_dict(self):
        return {
            'id': self.id,
            'company': self.company,
            'position': self.position,
            'city': self.city,
            'base_salary': float(self.base_salary),
            'months': self.months,
            'bonus': float(self.bonus),
            'signing_bonus': float(self.signing_bonus),
            'stock': self.stock,
            'type': self.type,
            'education': self.education,
            'tags': self.tags,
            'status': self.status,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S')
        }

class CredibilityVote(db.Model):
    __tablename__ = 'credibility_vote'

    id = db.Column(db.BigInteger, primary_key=True)
    salary_id = db.Column(db.BigInteger, nullable=False)
    user_id = db.Column(db.BigInteger, nullable=False)
    vote_type = db.Column(db.Enum('trust','distrust'), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

class Comment(db.Model):
    __tablename__ = 'comment'

    id = db.Column(db.BigInteger, primary_key=True)
    salary_id = db.Column(db.BigInteger, nullable=False)
    user_id = db.Column(db.BigInteger, nullable=False)
    content = db.Column(db.Text, nullable=False)
    is_anonymous = db.Column(db.Integer, default=1)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

class Benefit(db.Model):
    __tablename__ = 'benefit'

    id = db.Column(db.Integer, primary_key=True)
    salary_id = db.Column(db.BigInteger, nullable=False)
    type = db.Column(db.Enum('housing','meal','transport','health','other'), nullable=False)
    description = db.Column(db.String(255))


def to_dict(self):
    return {
        'id': self.id,
        'company': self.company,
        'position': self.position,
        'city': self.city,
        'base_salary': float(self.base_salary),
        'months': self.months,
        'bonus': float(self.bonus) if self.bonus else 0,
        'signing_bonus': float(self.signing_bonus) if self.signing_bonus else 0,
        'stock': self.stock,
        'type': self.type,
        'education': self.education,
        'industry': self.industry,
        'remark': self.remark,
        'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S')
    }